package com.buaa.util.etl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import com.buaa.util.FilterUtils;
import com.buaa.util.SymbolUtils;

public class InstrumentUtils {
	/**
	 * Instrument : "单位名称"(1)
	 * 
	 * @param unitname
	 * @return
	 */
	public static String getUnit_name(String unit_name) {
		if (unit_name == null || unit_name.equals(""))
			return "";
		Set<Character> symbol = new HashSet<Character>();
		symbol.add('(');
		symbol.add(')');
		return FilterUtils.characterFilter(unit_name, symbol, "", 0);
	}

	/**
	 * Instrument : "单位编码"(2)
	 * 
	 * @param unitcode
	 * @return
	 */
	public static Long getUnit_code(String unit_code) {
		if (unit_code == null || unit_code.equals(""))
			return -1L;
		return FilterUtils.longFilter(unit_code);
	}

	/**
	 * Instrument : 类型（3）
	 * 
	 * @param type
	 * @return
	 */
	public static Byte getType(String type) {
		if (type == null || type.equals(""))
			return -1;
		type = type.trim();
		switch (type) {
		case "仪器":
			return 1;
		case "设备":
			return 2;
		case "设施":
			return 3;
		default: // 其他
			return 4;
		}
	}

	/**
	 * Instrument : 型号（4）
	 * 
	 * @param model
	 * @return
	 */
	public static String getModel(String model) {
		if (model == null || model.equals(""))
			return "";
		return model.trim();
	}

	/**
	 * Instrument : 购置时间（5）
	 * 
	 * @param buytime
	 * @return
	 * @throws ParseException
	 */
	public static Date getBuy_time(String buy_time) throws ParseException {
		if (buy_time == null || buy_time.equals(""))
			return new SimpleDateFormat("yyyy-MM-dd").parse("1000-01-01");
		return FilterUtils.dateFilter(buy_time, "yyyy/MM/dd");
	}

	/**
	 * Instrument : 原值金额（6）
	 * 
	 * @param orgvalue
	 * @return
	 */
	public static Float getOrg_value(String org_value) {
		if (org_value == null || org_value.equals(""))
			return -1F;
		org_value = org_value.replace("万", "");
		return FilterUtils.floatFilter(org_value);
	}

	/**
	 * Instrument : 币种(7)
	 * 
	 * @param currency
	 * @return
	 */
	public static String getCurrency(String currency) {
		if (currency == null || currency.equals(""))
			return "";
		String deal = FilterUtils.characterFilter(currency, null, "", 0);
		switch (deal) {
		case "人民币":
			return "A001";
		case "美元":
			return "A002";
		case "欧元":
			return "A003";
		case "英镑":
			return "A004";
		case "日元":
			return "A005";
		case "港币":
			return "A006";
		case "加拿大元":
			return "A007";
		case "瑞士法郎":
			return "A008";
		case "瑞典克朗":
			return "A009";
		case "挪威克朗":
			return "A010";
		case "澳大利亚元":
			return "A011";
		case "丹麦克朗":
			return "A012";
		case "其他":
			return "A013";
		default:
			return "";
		}
	}

	/**
	 * Instrument : 外币折合成当时的人民币值（8）
	 * 
	 * @param orgvalue
	 * @return
	 */
	public static Float getRmb_value(String rmb_value) {
		if (rmb_value == null || rmb_value.equals(""))
			return -1F;
		return FilterUtils.floatFilter(rmb_value);
	}

	/**
	 * 主要技术指标（9）
	 * 
	 * @param qualification
	 * @return
	 */
	public static String getQualification(String qualification) {
		if (qualification == null || qualification.equals(""))
			return "";
		// Set<Character> symbol = new HashSet<Character>();
		// SymbolUtils.addAllSym(symbol);
		// return FilterUtils.characterFilter(qualification, symbol, "", 4);
		return qualification.trim();
	}

	/**
	 * 主要附件（10）
	 * 
	 * @param accessory
	 * @return
	 */
	public static String getAccessory(String accessory) {
		if (accessory == null || accessory.equals(""))
			return "";
		// Set<Character> symbol = new HashSet<Character>();
		// SymbolUtils.addAllSym(symbol);
		// return FilterUtils.characterFilter(accessory, symbol, "", 5);
		return accessory.trim();
	}

	/**
	 * 功能/应用范围(11)
	 * 
	 * @param function
	 * @return
	 */
	public static String getFunction(String function) {
		if (function == null || function.equals(""))
			return "";
		// Set<Character> symbol = new HashSet<Character>();
		// SymbolUtils.addAllSym(symbol);
		// return FilterUtils.characterFilter(function, symbol, "", 5);
		return function.trim();
	}

	/**
	 * 接待时间（工作日）(12)
	 * 
	 * @param weekday
	 * @return
	 */
	public static String getWeekday(String weekday) {
		if (weekday == null || weekday.equals(""))
			return "";
		return weekday.trim();
	}

	/**
	 * 仪器所在部门(13)
	 * 
	 * @param depart_name
	 * @return
	 */
	public static String getDepart_name(String depart_name) {
		if (depart_name == null || depart_name.equals(""))
			return "";
		// 两个空格分别为“半角空格”和“全角空格”
		depart_name = depart_name.replace(" ", "").replace("　", "").replace("+", "").replace("\"", "").replace("·", "")
				.replace("_", "").replace("\t", "").replace("/", ",");
		Set<Character> symbol = new HashSet<Character>();
		symbol.add('(');
		symbol.add(')');
		symbol.add('-');
		symbol.add(',');
		symbol.add('#');
		SymbolUtils.addArabicSym(symbol);
		return FilterUtils.characterFilter(depart_name, symbol, "", 4);
	}

	/**
	 * 仪器设施联系人姓名(14)
	 * 
	 * @param contact_name
	 * @return
	 */
	public static String getContact_name(String contact_name) {
		if (contact_name == null || contact_name.equals(""))
			return "";
		contact_name = contact_name.replace("，", "、").replace("/", "、").replace("？", "").replace(" ", "");
		Set<Character> symbol = new HashSet<Character>();
		symbol.add('、');
		return FilterUtils.characterFilter(contact_name, symbol, "", 1);
	}

	/**
	 * 仪器设备联系人电话(15)
	 * 
	 * @param contact_phone
	 * @return
	 */
	public static String getContact_phone(String contact_phone) {
		if (contact_phone == null || contact_phone.equals(""))
			return "";
		switch (contact_phone) {
		case "82191545 13503631076":
			contact_phone = "82191545/13503631076";
			break;
		case "0468-3388078 13359972588":
			contact_phone = "0468-3388078/13359972588";
			break;
		case "0451-- 58779103":
			contact_phone = "0451-58779103";
			break;
		case "0451-86282085 0451-86282985":
			contact_phone = "0451-86282085/0451-86282985";
			break;
		case "13654500223   86414867":
			contact_phone = "13654500223/86414867";
			break;
		case "13895757262（82192311）":
			contact_phone = "13895757262/82192311";
			break;
		default:
			break;
		}
		contact_phone = contact_phone.replace("转", "-").replace("，", "/").replace(",", "/").replace("、", "/")
				.replace("--", "-").replace("－", "-").replace("—", "-").replace("  ", "/").replace("?", "-")
				.replace(" ", "-");
		return FilterUtils.phoneFilter(contact_phone, "");
	}

	/**
	 * 仪器图片（16）
	 * 
	 * @param pic
	 * @return
	 */
	public static String getPic(String pic) {
		if (pic == null || pic.equals(""))
			return "";
		String[] suffixes = { ".jpg", ".JPG", ".bmp", ".BMP", ".jpeg", ".JPEG", ".gif", ".GIF", ".png", ".PNG" };
		return FilterUtils.fileFilter(pic, suffixes);
	}

	/**
	 * Instrument : 仪器中文名称(17)
	 * 
	 * @param name_zh
	 * @return
	 */
	public static String getName_zh(String name_zh) {
		if (name_zh == null || name_zh.equals(""))
			return "";
		// name_zh = StringUtils.full2Half(name_zh);
		name_zh = name_zh.trim().replace("(", "（").replace(")", "）").replace("—", "-").replace("－", "-")
				.replace("―", "-").replace("\\", "/").replace("", "").replace("", "").replace("　", "")
				.replace("“", "").replace("：", ":").replace("\"", "”");
		Set<Character> symbol = new HashSet<Character>();
		symbol.add(',');
		symbol.add(' ');
		symbol.add('、');
		symbol.add('/');
		symbol.add('.');
		symbol.add('-');
		symbol.add('(');
		symbol.add(')');
		symbol.add('+');
		symbol.add(':');
		symbol.add('“');
		symbol.add('”');
		symbol.add('&');
		symbol.add('Ф');
		symbol.add('α');
		symbol.add('β');
		symbol.add('γ');
		symbol.add('И');
		symbol.add('К');
		symbol.add('Ⅲ');
		symbol.add('#');
		symbol.add(';');
		SymbolUtils.addArabicSym(symbol);
		return FilterUtils.characterFilter(name_zh, symbol, "", 4);
	}

	/**
	 * 生产国家(18)
	 * 
	 * @param home
	 * @return
	 */
	public static String getHome(String home) {
		if (home == null || home.equals(""))
			return "";
		return FilterUtils.characterFilter(home, null, "", 0);
	}

	/**
	 * 仪器使用状态(19)
	 * 
	 * @param status
	 * @return
	 */
	public static String getStatus(String status) {
		if (status == null || status.equals("")) {
			return "其他";
		}
		status = status.trim();
		if(status.equals("正常使用")||status.equals("维修")||status.equals("限制范围使用"))
			return status;
		return "其他";
//		switch (status) {
//		case "正常使用":
//			return 1;
//		case "维修":
//			return 2;
//		case "限制范围使用":
//			return 3;
//		default:
//			return 0;
//		}
	}

	/**
	 * 资产管理编号(20)
	 * 
	 * @param manage_code
	 * @return
	 */
	public static String getManage_code(String manage_code) {
		if (manage_code == null || manage_code.equals(""))
			return "";
		manage_code = manage_code.trim();
		// String regEx = "^\\d{8}$";
		// return FilterUtils.patternFilter(manage_code, regEx, "");
		manage_code = manage_code.replace("、", "/").replace("—", "-").replace("？", "").replace("", "");
		Set<Character> symbol = new HashSet<Character>();
		SymbolUtils.addArabicSym(symbol);
		symbol.add('/');
		symbol.add('.');
		symbol.add(' ');
		symbol.add('-');
		symbol.add('+');
		symbol.add('(');
		symbol.add(')');
		symbol.add('Ⅲ');
		symbol.add('Ⅱ');
		symbol.add('*');
		symbol.add(';');
		return FilterUtils.characterFilter(manage_code, symbol, "", 2);
	}

	/**
	 * Instrument : 仪器英文名称(21)
	 * 
	 * @param name_zh
	 * @return
	 */
	public static String getName_en(String name_en) {
		if (name_en == null || name_en.equals(""))
			return "";
		name_en = name_en.replace("（", "(").replace("）", ")").replace(" ", " ").replace("，", ",").replace("　", " ")
				.replace("－", "-").replace("—", "-").replace("―", "-").replace("“", "\"").replace("”", "\"")
				.replace("、", ",").replace("？", "").replace("’", "'").replace("·", ".");
		Set<Character> symbol = new HashSet<Character>();
		symbol.add(' ');
		symbol.add('(');
		symbol.add(')');
		symbol.add('/');
		symbol.add('\\');
		symbol.add(':');
		symbol.add(';');
		symbol.add('-');
		symbol.add('.');
		symbol.add('_');
		symbol.add('+');
		symbol.add('&');
		symbol.add('#');
		// symbol.add('’');
		symbol.add(',');
		symbol.add('*');
		symbol.add('α');
		symbol.add('β');
		symbol.add('μ');
		symbol.add('Ⅱ');
		symbol.add('Ⅲ');
		symbol.add('"');
		symbol.add('\'');
		SymbolUtils.addArabicSym(symbol);
		return FilterUtils.characterFilter(name_en, symbol, "", 4);
	}

	/**
	 * 自筹（22）
	 * 
	 * @param raise
	 * @return
	 */
	public static Float getRaise(String raise) {
		if (raise == null || raise.equals(""))
			return -1F;
		raise = raise.replace("万元", "").replace("万", "").replace("=", "").replace("＝", "").replace("、", "");
		return FilterUtils.floatFilter(raise);
	}

	/**
	 * 仪器分类编码(23)
	 * 
	 * @param type_code
	 * @return
	 */
	public static String getType_code(String type_code) {
		if (type_code == null || type_code.equals(""))
			return "";
		type_code = type_code.trim();
		String regEx = "^\\d{6}$";
		return FilterUtils.patternFilter(type_code, regEx, "");
	}

	/**
	 * 制造商(24)
	 * 
	 * @param manufacturer
	 * @return
	 */
	public static String getManufacturer(String manufacturer) {
		if (manufacturer == null || manufacturer.equals(""))
			return "";
		return manufacturer.trim();
	}

	/**
	 * 其他(25)
	 * 
	 * @param other
	 * @return
	 */
	public static Float getOther(String other) {
		if (other == null || other.equals(""))
			return -1F;
		other = other.replace("　", "");
		return FilterUtils.floatFilter(other);
	}

	/**
	 * 仪器所在地区（市）(26)
	 * 
	 * @param city
	 * @return
	 */
	public static String getCity(String city) {
		if (city == null || city.equals(""))
			return "";
		city = city.trim();
		if (city.startsWith("大兴安岭")) {
			city = "大兴安岭地区";
		} else if (!city.endsWith("市"))
			city += "市";
		return city;
	}

	/**
	 * 所在研发基地类型（27）
	 * 
	 * @param base_type
	 * @return
	 */
	public static String getBase_type(String base_type) {
		if (base_type == null || base_type.equals(""))
			return "";
		if (base_type.endsWith(","))
			base_type = base_type.substring(0, base_type.length() - 1);
		return base_type.trim();
	}

	/**
	 * 所在研发基地名称（28）
	 * 
	 * @param base_name
	 * @return
	 */
	public static String getBase_name(String base_name) {
		if (base_name == null || base_name.equals(""))
			return "";
		base_name = base_name.trim();
		if (base_name.endsWith(";"))
			base_name = base_name.substring(0, base_name.length() - 1);
		if (base_name.equals("无@0"))
			return "";
		base_name.replace(" ", "");
		String regEx = "^[\u4E00-\u9FA5]+@[\u4E00-\u9FA5]+(;[\u4E00-\u9FA5]+@[\u4E00-\u9FA5]+)*$";
		return FilterUtils.patternFilter(base_name, regEx, "");
	}

	/**
	 * 仪器设备联系人传真(29)
	 * 
	 * @return
	 */
	public static String getContact_fax(String contact_fax) {
		if (contact_fax == null || contact_fax.equals(""))
			return "";
		switch (contact_fax) {
		case "0451-55153030  55153678":
			contact_fax = "0451-55153030/55153678";
			break;
		case "0451 8641 2236":
			contact_fax = "0451-86412236";
			break;
		case "451-82519222":
			contact_fax = "0451-82519222";
			break;
		case "86-451-86402972":
			contact_fax = "0451-86402972";
			break;
		default:
			break;
		}
		contact_fax = contact_fax.replace(" ", "-").replace("－", "-").replace("—", "-").replace("?", "-").replace("--",
				"-");
		return FilterUtils.faxFilter(contact_fax);
	}

	/**
	 * 仪器设备联系人email(30)
	 * 
	 * @param contact_email
	 * @return
	 */
	public static String getContact_email(String contact_email) {
		if (contact_email == null || contact_email.equals(""))
			return "";
		contact_email = contact_email.replace(",", ".").replace("E-mail", "").replace(" ", "");
		return FilterUtils.emailFilter(contact_email);
	}

	/**
	 * 仪器设备联系人通讯地址(31)
	 * 
	 * @return
	 */
	public static String getContact_addr(String contact_addr) {
		if (contact_addr == null || contact_addr.equals(""))
			return "";
		if (contact_addr.startsWith("哈尔滨工业大学科学园2A栋304室"))
			contact_addr = "哈尔滨工业大学科学园2A栋304室";
		else if (contact_addr.endsWith("哈尔滨市松北区浦源路2468号")) {
			contact_addr = "哈尔滨市松北区浦源路2468号";
		} else if (contact_addr.startsWith("山东省威海市")) {
			contact_addr = contact_addr.substring(6);
		}
		contact_addr = contact_addr.trim().replace("＃", "号").replace("#", "号").replace(" ", "").replace("+", "")
				.replace(",", "，").replace("　", "").replace("—", "-").replace("―", "-").replace("？", "-")
				.replace("\"", "").replace(":", "");
		if (contact_addr.startsWith("黑龙江")) {
			if (contact_addr.charAt(3) != '省') {
				contact_addr = "黑龙江省" + contact_addr.substring(3);
			}
		} else {
			contact_addr = "黑龙江省" + contact_addr;
		}

		Set<Character> symbol = new HashSet<Character>();
		symbol.add('-');
		symbol.add('(');
		symbol.add(')');
		symbol.add(',');
		symbol.add('/');
		symbol.add('、');
		SymbolUtils.addArabicSym(symbol);
		return FilterUtils.characterFilter(contact_addr, symbol, "", 4);
	}

	/**
	 * 仪器设备联系人邮编(32)
	 * 
	 * @param contact_zipcode
	 * @return
	 */
	public static String getContact_zipcode(String contact_zipcode) {
		if (contact_zipcode == null || contact_zipcode.equals(""))
			return "";
		return FilterUtils.zipcodeFilter(contact_zipcode);
	}

	/**
	 * 是否进口（33）
	 * 
	 * @param is_import
	 * @return
	 */
	public static Boolean getIs_import(String is_import) {
		// 空设定为国内
		if (is_import == null || is_import.equals(""))
			return false;
		if (is_import.trim().equals("国外"))
			return true;
		return false;
	}

	/**
	 * 服务行业类别(34)
	 * 
	 * @param industry_type
	 * @return
	 */
	public static String getIndustry_type(String industry_type) {
		if (industry_type == null || industry_type.equals(""))
			return "";
		industry_type = industry_type.replace(" ", "");
		industry_type = industry_type.replace("、", ",");
		String regEx = "^[\u4E00-\u9FA5]+(,[\u4E00-\u9FA5]+)*$";
		return FilterUtils.patternFilter(industry_type, regEx, "");
	}

	/**
	 * 主要学科领域 (35)
	 * 
	 * @param subject
	 * @return
	 */
	public static String getSubject(String subject) {
		if (subject == null || subject.equals(""))
			return "";
		subject = subject.replace(" ", "");
		subject = subject.toUpperCase();
		String regEx = "^\\w{2}\\d{2}(,\\w{2}\\d{2})*$";
		return FilterUtils.patternFilter(subject, regEx, "");
	}

	/**
	 * 仪器所在地区（省）(36)
	 * 
	 * @param province
	 * @return
	 */
	public static String getProvince(String province) {
		return "黑龙江省";
	}

	/**
	 * 仪器服务内容(37)
	 * 
	 * @param service_content
	 * @return
	 */
	public static String getService_content(String service_content) {
		if (service_content == null || service_content.equals("无") || service_content.equals(""))
			return "";
		return service_content.trim();
	}

}
